﻿using System;
using System.Collections.Generic;
using System.Text;
using CharacterTrailLibrary.Exceptions;
using CharacterTrailLibrary.Interface;
using CharacterTrailLibrary.Persist;
using CharacterTrailLibrary.Persist.DAL.Dao;
using Telegnosis.Framework.Constants;
using Telegnosis.Framework.DataAccess.DB;

namespace CharacterTrailLibrary.Impl
{
    public class BomHeaderPersist : IBomHeaderPersist
    {
        public void SaveBomHeader(Persist.BLL.Model.TBomHeaderModel bomHeaderModel)
        {
            var dao = new TBomHeaderDao();
            if (bomHeaderModel.EditFlag == Enums.EditFlag.New)
            {
                dao.InsertItem(bomHeaderModel.CreateOrm());
            }
            else if (bomHeaderModel.EditFlag == Enums.EditFlag.Update)
            {
                dao.UpdateItem(bomHeaderModel.CreateOrm());
            }
            bomHeaderModel.EditFlag = Enums.EditFlag.None;
        }

        public void SaveBomHeaders(List<Persist.BLL.Model.TBomHeaderModel> bomHeaderModels)
        {
            ITrans trans = DAHelper.GetSqlTrans();
            var dao = new TBomHeaderDao {Trans = trans};
            try
            {
                trans.Begin();
                foreach (var bomHeaderModel in bomHeaderModels)
                {
                    if (bomHeaderModel.EditFlag == Enums.EditFlag.New)
                    {
                        dao.InsertItem(bomHeaderModel.CreateOrm());
                        bomHeaderModel.EditFlag = Enums.EditFlag.None;
                    }
                    else if (bomHeaderModel.EditFlag == Enums.EditFlag.Update)
                    {
                        dao.UpdateItem(bomHeaderModel.CreateOrm());
                        bomHeaderModel.EditFlag = Enums.EditFlag.None;
                    }
                }
                trans.Commit();
            }
            catch (Exception exception)
            {
                trans.RollBack();
                throw ;
            }
        }

        public Persist.BLL.Model.TBomHeaderModel GetTBomHeaderModel(string rowGuid)
        {
           	var strWhere="";
			var sqlWhere=new SqlItem();
            strWhere +=String.Format( " 1=1 and RowGuid='{0}'",rowGuid);
            sqlWhere.Text = strWhere;
            var dao = new TBomHeaderDao();
            var list = dao.GetList(sqlWhere);
            if (list.Count > 0)
                return list[0];
            return null;
        }

        public List<Persist.BLL.Model.TBomHeaderModel> GetTBomHeaderModels(string condition)
        {
            var sqlWhere = new SqlItem();
            sqlWhere.Text =  condition + " order by ParentItemNumber";
            var dao = new TBomHeaderDao();
            return dao.GetList(sqlWhere);
        }


        public void DeleteBomHeaders(List<Persist.BLL.Model.TBomHeaderModel> bomHeaderModels)
        {
            ITrans trans = DAHelper.GetSqlTrans();
            var dao = new TBomHeaderDao { Trans = trans };
            try
            {
                trans.Begin();
                foreach (var bomHeaderModel in bomHeaderModels)
                {
                    if (bomHeaderModel.EditFlag == Enums.EditFlag.Delete)
                    {
                        dao.DeleteItem(bomHeaderModel.CreateOrm());
                    }
                    else
                    {
                        throw new AppException("要删除的对象没有将EditFlag置为Delete！");
                    }
                }
                trans.Commit();
            }
            catch (Exception exception)
            {
                trans.RollBack();
                throw;
            }
        }
    }
}
